package com.xdtech.base.service.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.base.dao.CityDao;
import com.xdtech.base.model.City;
import com.xdtech.base.service.CityService;
import com.xdtech.base.vo.CityItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-06-02 14:02:21
 * @since 1.0
 * @see
 */
@Service
public class CityServiceImpl implements CityService {
	private Log log = LogFactory.getLog(CityServiceImpl.class);
	@Autowired
	private CityDao cityDao;
	@Autowired
	private BaseDao baseDao;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:21
	 * @modified by
	 * @param entity
	 */
	public void save(City entity) {
		cityDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:21
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<City> entities) {
		cityDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:21
	 * @modified by
	 * @param entity
	 */
	public void delete(City entity) {
		cityDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:21
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		cityDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:21
	 * @modified by
	 * @param id
	 * @return
	 */
	public City get(Long id) {
		return cityDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:21
	 * @modified by
	 * @return
	 */
	public List<City> getAll() {
		return cityDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:21
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> citys = null;
		long rows = 0;
		if (pg!=null) {
			Page<City> page = cityDao.findPage(pg,"from City order by createTime desc", values);
			citys = BeanUtils.copyListProperties(
					CityItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<City> cityList = cityDao.find("from City order by id desc", values);
			citys = BeanUtils.copyListProperties(
					CityItem.class, cityList);
			rows = citys.size();
		}
		results.put("total", rows);
		results.put("rows", citys);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:21
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateCity(CityItem item) {
		City city = null;
		if (item.getId()==null) {
			city = new City();
		}else {
			city = cityDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(city, item);
		cityDao.save(city);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:21
	 * @modified by
	 * @param cityId
	 * @return
	 */
	public CityItem loadCityItem(Long cityId) {
		City city = cityDao.get(cityId);
		CityItem cityItem = new CityItem();
		BeanUtils.copyProperties(cityItem, city);
		return cityItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:21
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteCityInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:21
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteCityInfo(List<Long> cityIds) {
		for (Long id : cityIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-06-02 14:02:21
	 * @modified by
	 * @return
	 */
	@Override
	public List<CityItem> loadItems() {
		List<City> citys = getAll();
		return CityItem.createItems(citys);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-6-2下午9:29:20
	 * @modified by
	 * @param provinceId
	 * @return
	 */
	@Override
	public List<City> loadByProvinceId(String provinceId) {
		List<City> cities = null;
		if (StringUtils.isBlank(provinceId)) {
			cities = getAll();
		}else {
			cities = cityDao.findBy("provinceId", Long.valueOf(provinceId));
		}
		return cities;
	}

}
